package com.sinosoft.cloud.access.abc.crypto;

import com.sinosoft.cloud.access.annotations.AccessCrypto;
import com.sinosoft.cloud.access.crypto.AesUtil;
import com.sinosoft.cloud.access.crypto.CryptoBean;
import com.sinosoft.cloud.access.crypto.CryptoType;

@AccessCrypto(name = "abc", cryptoType = CryptoType.AES)
public class AbcCryptoBean extends CryptoBean {


    /**
     * 加密
     *
     * @param msg
     * @return
     */
    @Override
    public String encryt(String msg) {
//        if (msg.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")){
//            return msg;
//        }
        switch (getCryptoType()) {
            case AES:
                String result = AesUtil.encode(msg, getKey(), "UTF-8");
                return result;
        }
        return null;
    }

    /**
     * 农银解密逻辑是 0-73 位作为报文头，此处是不加密的，从第74位开始是报文体
     *
     * @param msg
     * @return
     */
    @Override
    public String decryt(String msg) {
//        byte[] body = getCryptoBody(msg, 74, msg.length());
        //为了方便测试，自己传的报文没有农行的报文头，所以直接返回，不进行处理
        if (msg.startsWith("<ABCB2I>")) {
            return msg;
        }
        //农行传输的报文切掉前面的73个字符
        if (msg.startsWith("X1.")) {
            msg = msg.substring(73);
        }
        //切掉后，判断是否为加密报文，如果不是，则直接返回，如果是，则进行解密
        //暂时得到的解密报文是乱码
        if (msg.startsWith("<")) {
            return msg;
        }
        switch (getCryptoType()) {
            case AES:
                String result = AesUtil.decode(msg, getKey(), "UTF-8");
                return result;
        }
        return null;
    }

    public static void main(String[] args) {
        String x = "X1.0000094403002    1 0                                          0000000019BA7B041463598D3D668716EF1E25B9E40EB28F298B965AA68A59C4C082FF7F234BB59C0DD88FDD618775CF0C06A77F47D866C048BB4ED0C3FE521782363EE95C0CE3ADBEEA000BDCFC206C025A97A6AB7BF8FC014FBD07A3A8C05E8FCA008632003D0BF26FAC735E7DF1272940F16D02D6C2AEE9716FF72EAD762D621BBF70CBD0E2A739711DBEF4D9B1C263F1E3B35E8E54312EDA11C1BBE162331716336861B65EE55B45EA4E65F676C8C2F0B1A646EFC18A2E48CB4E1DF0E4203264B59ED47C1322F8D1DB6D21557C87AB81791D618BD2D1CC8CA23A2A7178E58C7AC42773E1D0993FDC4F6349005F7BB61817019A7E868D17D54FD1E061A4D44DA993DAB059704F22198E1AE46F6947D87271473BAE69022D91CD8EED26C24852BC6B6280ED601CA8C8993299A77B401DE7B49A7D6B7C3B1BF29B1FAE8DA9AE81280ECA5A19B18FDD5C788396C4972AEEDD30047620BD82E2BC37D928A2D8991DC176EC5839CB84C34243FB7FFAAEF45F550438FF178A29F99CAD9EFA15AE2652D35CA5F6ECF891E762C135A86A9820B621F7B936FDE784B3C7C5B848D26135D44D6900501C668FF467F03B0C019413C5A56A61D1846960A38DC17AB52ACD1532E4A58742284CCDE5324194D001FE18B0171E18A31201BC5441CF6C7B3831D08C59CF79223745A31AC5B5E9C36922AD32DFB077B7F67EA9EB758A6A34E1F25EAF6CAD1D5FAADB3B7A262044583DC7D0D3DFAD675865459D4FA1CB8983B10B576C218B7E098EF8660D00648EF557CB69C66FA532AD61F5678803750AFEA0008AE1D036EA19E8646DA4348CD1449C84C73FE1EFAC39F21904467EB92F668D4EC2B8E15ED4D98C50EAA2C3CBDC082B3150D331FC74E9CAFFD533B7CDC2C8FD4E2098382BDA243654AFF20109C659A00D3138B9038C870BC994F7F62EDE6B5602E8DC163B4069F7E0F95CF2CA8C5C9619250552C4D217DF47EF36E3918D28FB071B5BDE6EC33C14A26D4C28C8FECC4F9692C7838CC2EB13153FC141DE482E65E75F8BA832A5FE445BCC82E9C49F2D10AEA97D641C2EA036D151D94AC3C92A30EF321515B7AD40A822EE19A383CF1473734BAEFE04A1EDAAB858B7170F5C41CA649F0E2CF6FD187C79F338BBEB4481728863AFDB07DC39FD7229DDB8F3F8AC2E19F9CB3E9874CF8C8501AC18E7D7BFD1F3E7CCAC997279D750B7434BAE05A778ECF3728ED97CD1635D8778002A23D7CC96EBE67BCA300F17122C75A062E879D5306074834B856A2CFDD760B531658C7E0C2B2584F2357DCABC4DACE55BC309A87782E801C5AB30F65DF79428407A53C83FAC08AD66EEDC292316BAF0F0BA9A9D5C5DA32E51AA17B8A3242C430319F5327090893307F811CD74BCE833FAC53EDB56AB8723608E445A9230247C33EBCBCE9D692F5991B9B24C6F6F70B572AE7B713C406B033481A77AA4DB3C4661E52866C2562FBF0C4762A51C5EFFC080966D1949C3244B50D9A003F0AD975ABFBC3A1D7BAE53BCC76BB88DBBD75DC922EAFF4E1E8AD3A9A325E6BF1D3D7FEF64E058333EADA2417769F45D3B719103B4726BC11CABE3D1B1FE7664B7D5FAD3F5A87926C3C346AA7DEECEC58736E63133DDF3AEA67564F31D964E40B3353CEA9F8357179CFF5F8F63E1BB2178AADFE27D8DA51246894B0848A29CDD03EF898A9BD594ECA5DA3DC51F636DF269C8238FC53E9F97DC3ABA0A38FE18EE1FBFD5CF7A44273734A3EC8DC897FF8E1C61C8A78C136859F35BFA3909AABD4C9A29CB0CA0459A99A2347857B53B75ABDC460DEB2A82F4B0152734BEE82EF65D363321256175940B2F810767DA62C4A8963482F86F826023BAC9B1CBA7EE599A3DCEE6FE858106EF2238287C22085E17A8778C8D4135CA86DBC1D4E1B8C1568D7499FE902859663A1F0B99D3DAB08612DDEFF3EB21AE00009F5616C3768BAE53AC6109A5027872B67234E3E313E860232DFBE02AD55733EB4723C789923C686F4AAAA516E7569B06F745FB7E22886EF6C59577E6633287D0E0D71E03B00B042FBA6A941A5B34EBC5F3DFB4DAECCCBBA624C230A75ED94469DE20C4C415D20B6436F96C9ACFD25F1A3728D173DA4BCA16A6681C4712BB1000ADD9039F96772BC852B3248BF9727600B2039AEEAC5B79CDF111756522297109084729F82FEA73B8FBC300CE5C166B44984B5F6B092A75CB3E5E335DE3DB1E3D5CC527ECB6AD7D523ECE813A641929D426C054AA789D6988F878FCE42C3794F0B3D61A936BB94AAB360E72DECCBB2B897284B4861627C732F771609E55630242B15CBA80CE4E80359E1ADE5581AF4935C39FDF301E729E25C04073684842D0B1A2D5FF24ED5F39FE5A9D10A529BB1BEBE7195B2F62614A42EB6EC1F54BEF94503F44066831F634B0122757B3C4296538DF58D6484C6BECF71A51CD944D730586B522EB6A1B962373E4E178D22E179A7E830B54FDF66933EC5793713537EF3B23BE5827017CE611E2142FB3333B2AB35E09C7FED27C9AA30CBC31C8447ED88A534DFF3FE829C27BC49A777619092312778DCA3AD86F2D83D4B46596A979C003A052FCB5598BA15315E041CE4A1751C644466DD378DD3F9A82CCE20E554A7867E390F9FEE9ECF589800D5BF53B377A37E2C355F11FFD834538C35A8A0945B2C01329D36BA28F2F2A05EF2BA64E8487171EC83EAA2BE5F7073A3633D70E9224F4B89021127FCEBB73A1C5BED6BB6C0B395C2B98BA8E4A5E47DF9D83A3753E490E2132057B0770680D025CB753E9A538ECE0047C500E56AE7E0224CDCAA37C48DF1290C729BFDE191DD39D266DBD3A35C2A34AF376C1FD0BA7B7457674626BF6B32FFF12D0E3160CF45D93DE52AACC32F5E700572E2C2482DF15B07BA3C5148967D6394D19A105CB677CB80DDB0212A6B9C6A8F3A230F72A65A13344B6373FD22D3FAD23717AEE6FA1A3B17E2B0ADC43C1725F62B97393F5A23C4160DF4E353965DA8A25E23D116EB91F69A4B4D669D7084E91FB70F643364C5B268FAA151BF01EC0A1FD9E92C8B1275593B6FC53EF10B0035350173EC45FE90D1C394A7E1C382791C155A49183DA589BA81D09653D278B6CF80AB7CE879084AEFE5CDCBCEB02B4E24F1833BDB328A788B1DDDF887898770112D77A4B7C118C584167FBA671029E3D459E6D0C090A02416DA65E0C3B3E2EA5E62805CB9A868AD4C03D7D28931CAE1F49194AB8A5C50D3F0786A6B5297DD8E263473BB92BAAA05E4ED247400AA8BD75B81ACD68870421524B28EF542348D9A92552C7EBF63AF1E4DC5CD4445100CD7799876A2DDBF19236EB70B7D73B00AACC277571E3D33DA04E884F9F2D39E1101155A3CC4C9A94899BBC91F94E8BFF655F01CE87ADC6F25D1FF72C84993100859247011A1BD3FA9D24740DDB206A98753ED00C8332B86C90FD46502DF143BF37B1A1E8904BC0C061BAF4639499A0018885CD8559988EB429D28C28353BBFD5CF64990A6482E4251DEE1211A0CED39DC01AD1BF62658E08ADDB35092ED904BD0FA5756EEBD2FC2BA7A82C872E932067EDECA0B3BC711B39AD5EDDC7EC05FD413B1D3D2C46FB9F7F8A8FD025F9396D4FFD6351EBDC533325E247DAEE03F10CDB0BAF18C6C96EF5529ABFDED4BE0B8716BBA5B72DA0B53F92BFA7CEF6833A46F2F67C04E2D42445E02D23673AF9E78FAB00F60C66C03DE139ED700483BDA9A4615878E15C92383528AE0B8420CDFE898346D2C43CF83C541110E3A1BDFC88198A9818FFE3C7C54D9E62F688711FE195DFC609CBA363A3F093164FBD073D9E81B7184E2D811E3A099A02904E8E588D0D86160029D45888E2454C1A81E632E654F57AD68CDF7AD76E912279A6D9B5A45EBAD76A3C00401898D7FD00A0C3585D61C14612A54E51B4FB487E0AAA5589464B92FEAAF2BB2098E4F2DA423B675EAA3253799A51A76F2723D77E052CE9E18B236976C388976BC26801288AA4446D68D5CED7E2A19EF4515C068FE64660D037E2427189D1F2F81310E65EAFB7A4A4DBBA94D709F5AD95E30DB061CE0FBCC0D01A220788D457AE682AEEC3855E76FD6532895C3E3710E60A6410FC0A4BF35CF11CB36DF55F7A4A2EC84E118FD3193157BDED8E0164BABD4CA91DF94BBAE23AD1392DC1A48BDEFC96D5B459AEED03BB7BDA9042AC530A10515A6FB53418EDCADE60228BB5B7DF8160AAABED859AE074F0A2E7E525EA14086EE87268596CE98A7FFE9783C0BA34A16496201170E3FA69225385C619CD612650DEE11B9B4A5FB7ECCD2357E697EC97D2FE7218732CECC93414412B7F7F8832304EA2F7E31FAC881328A794EF19C0B24060C86D45D72C7ADD21AE4EAF6440F3ED08F03F79893F5AF5E62E8DA88A112B3C417F4E180F70497CD87792B437482EC222094E64386293285C93FD1884C82F1CB0894FD32E30CAAB3586115C7D8FCC5351F2DF6FF0EE22BACFB47558829C83AD3C457FFFC2D6A4DF3E9905E5392A64AABC8FFD0B24A658E2F283CC6E2A0A6D36643A859EDFB66AF3F162DE86E53167FBC54D6C02E3F21DD17EA0FBC1FFDD936E11249CBD36755D049B806DACFEA31D694CC5EBF4830679044FBA103EDE3F98A8C7121BDF55B540D8EFFF9B975D3382C61DCBDE76AD94C295BEFECF6FB8C39F9502487074A3E0B876E3689058A56A2E1F858D74936B39597292EEF9C34DD1CB397C1FA45D891AB6CD07B04924E8D76B2A62C2D2D7B81695883F55777289D3A81DB8ECA09C22E133FD938AB3D6B6775E2CCA458F92CA6B5876B3F6016212B50049FAEF1DE621276B5A99CDBB6B7A4DDF7B99B217B75D0A9FDD3E5FDF1A094C45B1FA951D60437569832256E4A4B6C86047547DC0F3F0CDA95DECBA1A8D859EBBB447C27C53233EDAE7149FEA8E185756E52D3942FB01946C792227B4306A30836ECD3ADEE27D00947F8A54EF28BA7950D94036D6BCB7A666E02924E9C88141BB481258FC2F340A627690F7EC6B2B241CE365CBB2D7B252485C1172A3FF09D0817BB4840C7A56D159CF8330424773962BBCF5E747AADAC4CADDAEBC5E5D5143715333A86921DAA5AA85EB77CC5C648D998805BF77D2FADE5A0D40B598E85755C59CBB902D0A6225BCD671230140592678AAA21D860B4C6105AA571EC3318B41C22A12FE8CDC2FD62494828AD2E352DA3B652ACEC67ED4080ADF961A841ACF72A4CA2D0F32DAF0F38AB870E34412B1974EC570B5623666CE6E1F643737DF2709763C99449B44A2EB64B11C96637A17D792EE419DBFBC43F0F04A756626244136E96529B5FC64C2C80AFF68E60780E79A7E88116195E9BE8D0D3EDA15AED5BCD26EEEDA768F3B556FC5376C20CA89E6D7D00775973C4159B386C443DC80CD4989928EC5C512573BAA3D67F1B4B5F83CCBCF5D4EDFBFBF52DACCE0D3043D951123509D0C97037CCCF431F5A94079C97D4B09684C3DC4ABA1722C74577BE01A01F739A3C18B64F49393D630043C2E6A22CACADE19534CCB0619EA56ABBA1E131104D6AE1EEC44FE656F200CC9D5D2636C6223B72B3DE681557E3D82CC3D8AFE687AE9CB6BE3F7BE449D43429C2E5BB683F8A2758D07928C2B84BD95152223E9A5041EEE662C166E8E3E235AA6666324D3BC1AB7E4B83045E93AD0F2675865030E00F23FC035E7F0A9F1AC4AD18034247D8AF5452EC8623BCDC9105784BEC8D69EA37355B6412A8633A5D819412AE090ED9B5D6B4E53B19AB651DB00C300A228BCE9B5F52F0FB7A48F5675018A7F1FA17B6B4C047245DE8588C47283814261DA377CC9D12D90FEAC3CE69F1DD8273956C8CBD7F3BC57A255639A142AE2828E11EC3735691025E7CFEEF62ACFAAC9150531541A99A1782ACD50F0453CCF7E8BAF307A7B7C5FEE709E17C506E7483AE3A9C3B76105485B40B70F7F7AA44D3FDE91F36031D1A1B5980197257D0715BED451E6475CFE181C3EF9824DABC7D07168FC26524C9704F2AAC6104E356D1A4C39E0592B88ECC6A942F6B74B798A682E93C0265E8D9A2E43E4E1F3DC48534165ACC906732B64E464D3DE618480CE7924DB83B04B3DFCE107AF1E0B2F55E54D78C136047180967A0B9AFF51B95B03F6DFFAB5017F5168F42B569FE40F259387BD169448E91B699F922B3A94ADD397A8167473436E37E85C8A98DB5862729FA11DAB492134561CF0E26105619E3A3CA3EDADA70C4CD492CE0CAAE36E8386CF5A18B038C46891C3F1CD56998B335B301B4838950FEC86063E836790598F2232C4A2DC441A0AB18FF568A89F5105F3D3C72AD17C445DEA7C7F1FD4D650CC819E86F5185A60C5A103211C7B403E44EF03643CB71CF84BA2BEA45F841B0BFF203D99197003C4EEFA9BE64CC6047C5299FF7E097FC89D026B8029263737AB6D14F44B13ECF1C99089925C3AEF0A3166798A2D2393F4025EC75D533EF020C1ED19D05D7B5678DE7C414B87016D82352145F4CA9D7BE66CEDA5A0B00DE05E32D6D829FA238A4248989CE77A344E3689877B994872F9BA0B6FA5B0D63D02F537957ABF9C52FAB4AC9BC8393B7C1BFCDF9E909C658787DB79A742B62A19B6ED715E94A384EF831B251FCDF01731302E2724F77119B010C6002FEA0A24673EA89243F560C2A8E540B850FA1612154EAE68D98B065F8DE8EC06EB535BF395746312D763C6160030E6DB607BF17AB741A6C5254742BFD51B9201D41F6261C0";
        System.out.println(x.substring(73));
    }
}
